<template>
  <div>
    <h1>列表</h1>
    <div>
      <el-form :inline="true" :model="searchData" class="demo-form-inline">
        <el-form-item label="Approved by">
          <el-input
            v-model="searchData.keyWord"
            placeholder="Approved by"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button type="primary" @click="addShopList">添加</el-button>
          <el-button type="primary" @click="onExportExcel">数据导出</el-button>
        </el-form-item>
      </el-form>
      <div>
                <el-upload
    ref="uploadRef"
    class="upload-demo"
    :headers="headers"
    :on-success="handleExcelSuccess"
    action="http://localhost:10010/shop/index/importFile"
    :auto-upload="false"
  >
    <template #trigger>
      <el-button type="primary">请选择文件</el-button>
    </template>

    <el-button class="ml-3" type="danger" @click="submitUpload">
      开始导入
    </el-button>
  </el-upload>
      </div>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="id" />
      <el-table-column prop="name" label="商品名" />
      <el-table-column  label="商品图片" >
        <template #default="scope">
          <img v-if="scope.row.shopImg!=null" :src="scope.row.shopImg" style="width:75px; height: 50px;" :fit="fit" ></img>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格" />
      <el-table-column prop="statusName" label="状态" />
      <el-table-column prop="userName" label="用户名" />
      <el-table-column prop="typeName" label="xx" />
      <el-table-column prop="createTime" label="时间" />
      <el-table-column prop="content" label="内容" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            v-if="scope.row.status === 1"
            @click="checkShopStatus(scope.row)"
            >审核</el-button
          >
          <el-button
            type="danger"
            v-if="scope.row.status === 0"
            @click="onUpdateShopStatusDown(scope.row)"
            >下架</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div>
      <el-pagination
        v-model:current-page="searchData.pageNum"
        v-model:page-size="searchData.pageSize"
        :page-sizes="[3, 4, 5, 6]"
        :size="size"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加商品" width="500">
        <el-form :model="form">
          <el-form-item label="商品名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品价格" :label-width="formLabelWidth">
            <el-input v-model="form.price" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品类型" :label-width="formLabelWidth">
            <el-select
              v-model="form.shopType"
              placeholder="Please select a zone"
            >
              <el-option
                v-for="i in typeList"
                :label="i.name"
                :value="i.dictValue"
                :key="i.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="商品图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              :headers="headers"
              action="http://127.0.0.1:10010/shop/index/uploadFile"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">Cancel</el-button>
            <el-button type="primary" @click="onsubmitShop">
              Confirm
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        v-model="dialogStatusFormVisible"
        title="Shipping address"
        width="500"
      >
        <el-form :model="statusform">
          <el-form-item label="商品名称:" :label-width="formLabelWidth">
            {{ statusform.name }}
            <!-- <el-input v-model="statusform.shopName" autocomplete="off" /> -->
          </el-form-item>

          <el-form-item label="商品价格:" :label-width="formLabelWidth">
            {{ statusform.price }}
            <!-- <el-input v-model="statusform.shopPrice" autocomplete="off" /> -->
          </el-form-item>

          <el-form-item label="商品类型:" :label-width="formLabelWidth">
            {{ statusform.typeName }}
            <!-- <el-input v-model="statusform.typeName" autocomplete="off" /> -->
          </el-form-item>

          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-switch
              v-model="statusform.status"
              inline-prompt
              active-text="上架"
              inactive-text="下架"
              active-value="0"
              inactive-value="1"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogStatusFormVisible = false">取消</el-button>
            <el-button type="primary" @click="onUpdateShopStatus">
              修改
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import {
  getOrderList,
  updateOrder,
  deleteById,
  deleteAll,
  getShop,
  getShopType,
  addShop,
  updateShopStatus,
} from "../api/api";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";
import type { UploadInstance } from 'element-plus'

const uploadRef = ref<UploadInstance>()
const imageUrl = ref("");
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const headers = ref({
  token: sessionStorage.getItem("token"),
});
const form = ref({
  name: null,
  price: null,
  dictValue: null,
  shopType: null,
  shopImg:null,
});
import {
  ElMessage,
  ElMessageBox,
  type FormInstance,
  type FormRules,
} from "element-plus";
//导入
const submitUpload = () => {
  uploadRef.value!.submit()
}
const handleExcelSuccess: UploadProps["onSuccess"] = (
  res,
  uploadFile
) => {
if(res.code==200){
  ElMessage.success(res.data);
  getShopList();
}else{
  ElMessage.error(res.msg);
}
  // imageUrl.value=form.value.shopImg ;
  
};
//图片上传

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  res,
  uploadFile
) => {
imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  form.value.shopImg = res.data;
  // imageUrl.value=form.value.shopImg ;
  
};
import type { ComponentSize } from "element-plus";
import type { RefSymbol } from "@vue/reactivity";
const onsubmitShop = () => {
  
  addShop(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      getShopList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
//数据导出
const onExportExcel=()=>{
  console.log("导出数据");
  let url="http://localhost:9026/shop/index/exportExcel";
  window.open(url);
}
const size = ref<ComponentSize>("default");
const dialogStatusFormVisible = ref(false);
const tableData = ref([]);
const total = ref(0);
const searchData = ref({
  keyWord: null,
  pageNum: 1,
  pageSize: 3,
});
const statusform = ref({
  status: "",
  userId: null,
  shopType: null,

  typeName: null,
  statusName: null,
  userName: null,
});
const getShopList = () => {
  getShop(searchData.value).then((res) => {
    tableData.value = res.data.data.records;
    total.value = res.data.data.total;
  });
};

const onQuery = () => {
  console.log(searchData.value);
  getShopList();
};

const addShopList = () => {
  console.log("添加商品");
  dialogFormVisible.value = true;
};

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchData.value.pageSize = val;
  getShopList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchData.value.pageNum = val;
  getShopList();
};

const typeList = ref([]);
onMounted(() => {
  getShopList();
  //动态下拉数据
  getShopType(1).then((res) => {
    if (res.data.code == 200) {
      typeList.value = res.data.data;
    }
  });
});

//商品审核
const checkShopStatus = (row: any) => {
  //数据回显
  Object.assign(statusform.value, row);
  //弹框为显示
  dialogStatusFormVisible.value = true;
  console.log(row);
};

//下架
const onUpdateShopStatusDown = (row: any) => {
  Object.assign(statusform.value, row);
  statusform.value.status = 1;

  updateShopStatus(statusform.value).then((res) => {
    if (res.data.code == 200) {
      //修改成功
      ElMessage.success("修改成功！");
      dialogStatusFormVisible.value = false;
      //刷新列表
      getShopList();
    } else {
      //修改失败
      ElMessage.error(res.data.msg);
    }
  });
};

const onUpdateShopStatus = () => {
  console.log(statusform.value);

  updateShopStatus(statusform.value).then((res) => {
    if (res.data.code == 200) {
      //修改成功
      ElMessage.success("修改成功！");
      dialogStatusFormVisible.value = false;
      //刷新列表
      getShopList();
    } else {
      //修改失败
      ElMessage.error(res.data.msg);
    }
  });
};
</script>
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
